<?php
namespace app\controller;

use app\BaseController;
use \think\facade\Request;
use \think\facade\Cache;
use \think\facade\Db;
use \think\facade\Log;
use Qcloud\Sms\SmsSingleSender;

class Base extends BaseController
{
	// 公共修改
	public function baseUpdate()
	{
		  $param = Request::param();
		  $dd = $param['dd'];
		  $table_name = $param['table_name'];
		  
		  $dd['update_time'] = date("Y-m-d H:i:s");
		  
		  Db::name($table_name)
			  ->where('id',$dd['id'])
			  ->update($dd);
		  $this->returnJson(200,'操作成功',[]);
		  
	}
	// 公共添加
	public function baseAdd()
	{
		  $param = Request::param();
		  $dd = $param['dd'];
		  $table_name = $param['table_name'];
		  
		  $dd['add_time'] = date("Y-m-d H:i:s");
		  $dd['update_time'] = date("Y-m-d H:i:s");
		  
		  $id = Db::name($table_name)->insertGetId($dd);
		  $this->returnJson(200,'操作成功',['id'=>$id]);
		  
	}
	// 公共修改新增
    public function baseAddUpd()
    {
    	  $param = Request::param();
    	  $dd = $param['dd'];
    	  $table_name = $param['table_name'];
		
		  if($dd['id']){
			  $dd['update_time'] = date("Y-m-d H:i:s");
			  
			  Db::name($table_name)
			  			  ->where('id',$dd['id'])
			  			  ->update($dd);
			  $this->returnJson(200,'操作成功1',[]);
		  }else{
			  $dd['add_time'] = date("Y-m-d H:i:s");
			  $dd['update_time'] = date("Y-m-d H:i:s");
			  
			  $id = Db::name($table_name)->insertGetId($dd);
			  $this->returnJson(200,'操作成功2',['id'=>$id]);
		  }
		  
		  
    }
	// 公共删除
	public function baseDel()
	{
		  $param = Request::param();
		  $dd = $param['dd'];
		  $table_name = $param['table_name'];
		  
		  Db::name($table_name)
		  	->where('id',$dd['id'])
		    ->delete();
		  $this->returnJson(200,'操作成功',[]);
	}
	// 公共软删除
	public function baseSoftDel()
	{
		  $param = Request::param();
		  $dd = $param['dd'];
		  $table_name = $param['table_name'];
		  
		  Db::name($table_name)
		  	->where('id',$dd['id'])
			->useSoftDelete('delete_time',time())
		    ->delete();
		  $this->returnJson(200,'操作成功',[]);
	}
	// 公共分页软删除查询
	public function baseSoftSelect()
	{
		$param = Request::param();
		$table_name = $param['table_name'];
		$map = [];
		if(isset($param['map'])){
			$map = $param['map'];
		}
		
		
		$page=$param['page'];
		$pageSize = $param['pageSize'];
		$page=($page-1)*$pageSize;
		
		$result = Db::table($table_name)
			->where('cid',$param['cid'])
			->where($map)
			->order('id desc')
			->limit($page,$pageSize)
			->whereNull('delete_time')
		    ->select();
		$count = Db::table($table_name)
					 ->where($map)
					 ->where('cid',$param['cid'])
					 ->whereNull('delete_time')
		  			 ->count();
		$data = [];
		 $data['count']=$count;
		 $data['result']=$result;				
		$this->returnJson(200,'操作成功',$data);
		
	}
	// 公共查询
	public function baseFind()
	{
		$param = Request::param();
		$dd = $param['dd'];
		$table_name = $param['table_name'];
		$res = Db::table($table_name)
			->where('id',$dd['id'])
			->find();
		$this->returnJson(200,'操作成功',$res);
	}
	// 公共分页查询
	public function baseSelect()
	{
		$param = Request::param();
		$table_name = $param['table_name'];
		$map = [];
		if(isset($param['map'])){
			$map = $param['map'];
		}
		
		
		$page=$param['page'];
		$pageSize = $param['pageSize'];
		$page=($page-1)*$pageSize;
		
		$result = Db::table($table_name)
			->where('cid',$param['cid'])
			->where($map)
			->order('id desc')
			->limit($page,$pageSize)
		    ->select();
		$count = Db::table($table_name)
					 ->where($map)
					 ->where('cid',$param['cid'])
		  			 ->count();
		$data = [];
		 $data['count']=$count;
		 $data['result']=$result;				
		$this->returnJson(200,'操作成功',$data);
		
	}
	
}